package com.borqsprocessmanager.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.info.SectionInfo;
import com.borqsprocessmanager.util.DroidWallAPI.DroidApp;
import com.borqsprocessmanager.util.Log;

public class DroidWallAdapter extends SectionAdapter implements
        OnCheckedChangeListener, OnClickListener {
    private final String TAG = "DroidWallAdapter";

    private HashMap<String, SoftReference<Drawable>> iconCache = new HashMap<String, SoftReference<Drawable>>();

    private OnCheckedChangeListener mOnCheckedChangeListener;

    // private ArrayList<ReceiverProcessInfo> mList;
    private ArrayList<DroidApp> applications;
    private LayoutInflater mInflater;
    private PackageManager pm;

    private boolean isChanged = false;

    public DroidWallAdapter(Context context, ArrayList<DroidApp> applications) {
        this.applications = applications;
        mInflater = LayoutInflater.from(context);
        pm = context.getPackageManager();
        isChanged = false;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return applications.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        DroidApp info = applications.get(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.droid_wall_item, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView
                    .findViewById(R.id.droid_item_image_icon);
            holder.boxWifi = (CheckBox) convertView
                    .findViewById(R.id.droid_item_box_wifi);
            holder.box3g = (CheckBox) convertView
                    .findViewById(R.id.droid_item_box_3g);
            holder.box3g.setOnCheckedChangeListener(this);
            holder.box3g.setOnClickListener(this);
            holder.boxWifi.setOnCheckedChangeListener(this);
            holder.boxWifi.setOnClickListener(this);
            holder.appName = (TextView) convertView
                    .findViewById(R.id.droid_item_text_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.box3g.setTag(info);
        holder.boxWifi.setTag(info);
        SoftReference<Drawable> softIcon = iconCache.get(info.packageName);
        if (softIcon == null || softIcon.get() == null) {
            Drawable drawable;
            try {
                drawable = pm.getApplicationIcon(info.packageName);
            } catch (NameNotFoundException e) {
                drawable = App.mContext.getResources().getDrawable(
                        R.drawable.android);
            }
            softIcon = new SoftReference<Drawable>(drawable);
            iconCache.put(info.packageName, softIcon);
        }
        Drawable icon = softIcon.get();
        holder.icon.setImageDrawable(icon);
        if (info.packageName != null) {
            Log.v(TAG, "packageName===" + info.packageName);
            Log.v(TAG, "info.box3g===" + info.selected_3g);
            Log.v(TAG, "info.wifi===" + info.selected_wifi);
        }
        if (info.selected_3g) {// false 侧加入黑名单 没有勾选
            holder.box3g.setChecked(false);
        } else {
            holder.box3g.setChecked(true);
        }
        if (info.selected_wifi) {
            holder.boxWifi.setChecked(false);
        } else {
            holder.boxWifi.setChecked(true);
        }

        holder.appName.setText(info.names[0]);

        return convertView;

    }

    static class ViewHolder {
        TextView appName;
        ImageView icon;
        CheckBox boxWifi;
        CheckBox box3g;
    }

    // @Override
    // public void onCheckedChanged(CompoundButton buttonView, boolean
    // isChecked) {
    // this.isChecked = isChecked;
    // }

    public void setOnCheckedChanged(
            OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        final DroidApp app = (DroidApp) buttonView.getTag();
        if (app != null) {
            Log.v(TAG, "onCheckedChanged_PKG_name===" + app.packageName);
            switch (buttonView.getId()) {
            case R.id.droid_item_box_wifi:
                app.selected_wifi = !isChecked;// false 侧加入黑名单
                Log.v(TAG, "wifi_onCheckedChanged===" + isChecked);
                break;
            case R.id.droid_item_box_3g:
                app.selected_3g = !isChecked;// false 侧加入黑名单
                Log.v(TAG, "3g_onCheckedChanged===" + isChecked);
                break;
            }
        }
    }

    @Override
    public SectionInfo getSectionInfo(int posaction) {
        // TODO Auto-generated method stub
        return applications.get(posaction);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        isChanged = true;
    }

    @Override
    public void remove(int posaction) {
        Log.v(TAG, "remove");
        Log.v(TAG, "applications.size===" + applications.size());
        applications.remove(posaction - 1);
        Log.v(TAG, "applications.remove_size===" + applications.size());
    }

    public boolean isConfigChanged() {
        return isChanged;
    }

    @Override
    public String getHeadTitle(int posaction) {
        // TODO Auto-generated method stub
        return null;
    }

    // @Override
    // public void onClick(View v) {
    // mOnCheckedChangeListener
    // .onCheckedChanged((CompoundButton) v, isChecked);
    // }
}
